#include "common.h"

int minSubArrayLen(int target, vector<int>& nums)
{
	int n = nums.size();
	int left = 0;
	int right = 0;
	int sum = nums[0];
	int ans = n + 1;
	while (left <= right && right < n)
	{
		if (sum >= target)
		{
			ans = min(ans, right - left + 1);
			if (left < right)
			{
				sum -= nums[left];
				left++;
			}
			else
			{
				right++;
				if (right < n)
					sum += nums[right];
			}
		}
		else
		{
			right++;
			if (right < n)
				sum += nums[right];
		}
	}
	return ans == n + 1 ? 0 : ans;
}